﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using Ext.Net.Utilities;
using Ext.Net;
using MemberMgr.Data.BusinessObjects;
using MemberMgr.Data.ManagerObjects;
using MemberMgr.Website.Common;

namespace MemberMgr.Website
{
    public partial class _Default : PageBase
    {
        IManagerFactory managerFactory = new ManagerFactory();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!this.IsPostBack)
            {
                InitForm();
            }
            if (X.IsAjaxRequest)
            {
                this.ResourceManager1.DirectEventUrl = this.Request.Url.AbsoluteUri;

                this.Session[CommonConst.EXT_THEME] = Ext.Net.Theme.Default;
            }

        }

        private void InitForm()
        {
            this.lblUserName.Text = base.UserInfo.Username ?? "用户";
            this.lblUserName.Text += "。";

            this.GetExamplesNodes(this.exampleTree.Root);
        }

        protected Ext.Net.TreeNodeCollection GetExamplesNodes(Ext.Net.TreeNodeCollection nodes)
        {
            if (nodes == null)
            {
                nodes = new Ext.Net.TreeNodeCollection();
            }

            ItMenuManager menuManager = CommonUtil.managerFactory.GettMenuManager();

            IList<tMenu> list = menuManager.GetAll();

            ItRightManager rightMgr = Common.CommonUtil.managerFactory.GettRightManager();

            IList<tRight> rightlist = rightMgr.GetByROLEID(base.UserInfo.TRole.Id);

            list = list.Where(p => rightlist.Where(l => l.Rightno == p.Menuno).Count() > 0).ToList();

            IList<tMenu> topLevel = list.Where(p => p.Menulevel == "0").ToList();

            Ext.Net.TreeNode rootNode, leftNode;
            Ext.Net.TreeNode topNode = new Ext.Net.TreeNode("菜单");
            if (topLevel != null)
            {
                foreach (tMenu item in topLevel)
                {
                    rootNode = new Ext.Net.TreeNode();
                    rootNode.Text = item.Menuname;
                    rootNode.Icon = (Icon)Enum.Parse(typeof(Icon), item.Menuico);

                    IList<tMenu> leftLevel = list.Where(p => p.Menuparent == item.Id).ToList();

                    if (leftLevel.Count == 0)
                    {
                        continue;
                    }
                    foreach (tMenu left in leftLevel)
                    {
                        leftNode = new Ext.Net.TreeNode();
                        leftNode.Text = left.Menuname;
                        leftNode.Href = left.Menuurl;
                        leftNode.Leaf = true;
                        leftNode.Icon = (Icon)Enum.Parse(typeof(Icon), left.Menuico);
                        rootNode.Nodes.Add(leftNode);
                    }

                    topNode.Nodes.Add(rootNode);
                }
            }

            topNode.Expanded = true;
            nodes.Add(topNode);

            return nodes;
        }

        [DirectMethod(Namespace = "X")]
        public string GetThemeUrl(string theme)
        {
            Theme temp = (Theme)Enum.Parse(typeof(Theme), theme);

            this.Session[CommonConst.EXT_THEME] = temp;

            return temp == Ext.Net.Theme.Default ? "Default" : this.ResourceManager1.GetThemeUrl(temp);
        }

        [DirectMethod(Namespace = "X")]
        public static int GetHashCode(string s)
        {
            return Math.Abs("/Examples".ConcatWith(s).ToLower().GetHashCode());
        }

        [DirectMethod]
        protected void btnLogout_Click(object sender, EventArgs e)
        {
            this.Session.Abandon();
            this.Response.Redirect("Login.aspx",true);
        }

        [DirectMethod]
        protected void btnSavePassword_Click(object sender,DirectEventArgs e)
        {
            string passowrd1 = e.ExtraParams["password1"].ToString();
            string passowrd2 = e.ExtraParams["password2"].ToString();

            if (passowrd1 != passowrd2)
            {
                
            }
        }
    }
}
